package com.leadbank.baselbf.outils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.lead.security.encryption.SHA1.MessageDigestUtil;
import com.leadbank.baselbf.base.BaseApplication;
import com.leadbank.baselbf.log.LogBus;

import java.util.List;

/**
 * @ClassName: DeviceInfo
 * @Description:
 * @Author: zhouzhiguo
 * @Date: 2024/5/27 15:53
 * @Version: 5.0.6
 * 上海利得金融科技集团版权所有.
 */

public class DeviceInfo {
    private static String TAG = "DeviceInfo";

    /**
     * 获取设备唯一识别码
     * 由设备mac+id+序列号+内部识别码经过sha1计算取得
     * @return
     */
    public static String getDeviceUniqueId(Context context) {
        String value = getDeviceMac(context) + getDeviceID(context)+getDeviceSerialNo()+getDeviceFingerprint();
        String res = "";
        try {
            res = MessageDigestUtil.toSha1(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogBus.d(TAG,"getDeviceUniqueId = " + value);
        return res;
    }

    /**
     * 获取设备mac地址
     *
     * @return
     */
    public static String getDeviceMac(Context context) {
        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        String mac = wifi.getConnectionInfo().getMacAddress();
        LogBus.d(TAG,"getDeviceMac = " + mac);
        if (mac == null || mac.isEmpty()) {
            return "";
        }
        return mac;
    }


    /**
     * 获取设备id
     *
     * @return
     */
    @SuppressLint("MissingPermission")
    public static String getDeviceID(Context context) {
        String deviceId = "";
        try {
            if (Build.VERSION.SDK_INT < 29) {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                if (tm != null) {
                    if (TextUtils.isEmpty(tm.getDeviceId()) )
                        deviceId = tm.getDeviceId();

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(deviceId)){
            deviceId = Settings.Secure.getString(context.getApplicationContext().getContentResolver(),
                    Settings.Secure.ANDROID_ID);
        }
        return deviceId;

    }

    /**
     * 获取设备硬件序列号
     *
     * @return
     */
    public static String getDeviceSerialNo() {
        String sn = Build.SERIAL;
        LogBus.d(TAG,"getDeviceSerialNo = " + sn);
        if (sn == null || sn.isEmpty()) {
            return "";
        }
        return sn;
    }


    /**
     * 获取手机内部识别码
     *
     * @return
     */
    public static String getDeviceFingerprint() {
        return Build.FINGERPRINT;
    }



    @SuppressLint("MissingPermission")
    public static String getSimSerialNumber(Context context){
        String iccid = "";
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT < 29) {
            iccid = tm.getSimSerialNumber();
        }
        if (iccid == null || iccid.length() < 20) {

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                SubscriptionManager sm = SubscriptionManager.from(context);
                @SuppressLint("MissingPermission")
                List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();
                if (sis.size() >= 1) {
                    SubscriptionInfo si1 = sis.get(0); // 卡一
                    iccid = si1.getIccId();
                }
            }

        }
        return iccid;
    }

}
